﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;//添加刚下好的库
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace WindowsFormsAppDemo
{
    public partial class Form1 : Form
    {
        private const int DEFAULT_CHECK_CELL_NUM = 2;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          

        }

      

        private void textBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "请选择excel文件(*.xls)|*.xls";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
            Console.WriteLine("12212");
                   
                    Console.WriteLine("文件路径为:"+System.IO.Path.GetDirectoryName(dialog.FileName));

                    //userlist = ReadExcel.readexcel(dialog.FileName);
                    DataTable dataTable =  ReadExcelData(dialog.FileName);
                    dataTable.Rows.ToString();
                    int rowsCount =  dataTable.Rows.Count;
                    Console.WriteLine(System.IO.Path.GetDirectoryName(dialog.FileName) + "\\" + dataTable.Rows[0][0]);
                    for (int i = 0; i < rowsCount; i++)
                    {
                        /* for (int j = 0; j < 2; j++)
                         {
                             Console.WriteLine(dataTable.Rows[i][j]);
                         }*/

                        String a = System.IO.Path.GetDirectoryName(dialog.FileName) + "\\" +dataTable.Rows[i][0];
                        String b = System.IO.Path.GetDirectoryName(dialog.FileName) + "\\" + dataTable.Rows[i][1];
                        Console.WriteLine(a);
                        Console.WriteLine(b);
                        if (File.Exists(a))
                        {
                            File.Move(a, b);
                        }
                       
                    }
                    MessageBox.Show("改名完成");
                    /*  if (File.Exists(System.IO.Path.GetDirectoryName(dialog.FileName)+""))
                      {
                          File.Move(srcFileName, destFileName);
                      }*/
                    //Console.WriteLine(dataTable.Rows[0][1]);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;


                }
                textBox1.Text = dialog.FileName;
            }
        }

        /// <summary>
        /// 读取Excel中数据
        /// </summary>
        /// <param name="filePath"></param>
        private DataTable ReadExcelData(object filePath)
        {
            try
            {
                if (!File.Exists(filePath.ToString()))
                {
                    throw new Exception("文件不存在!");
                }
                DataTable dtExcel = InitDataTable();
                FileStream fsRead = new FileStream(filePath.ToString(), FileMode.Open);
                //创建工作薄
                IWorkbook workBook = new HSSFWorkbook(fsRead);
                //获取Sheet
                ISheet sheet = workBook.GetSheetAt(0);
                //获取Excel中的行数
                int ExcelRowsCount = sheet.LastRowNum;
                Assert.IsTrue(ExcelRowsCount == 1, "未读到Excel数据!");
                IRow currentRow;
                DataRow dr;
                for (int i = 0; i < ExcelRowsCount; i++)
                {
                    dr = dtExcel.NewRow();
                    //当前行数据
                    currentRow = sheet.GetRow(i);
                    SetCurrentRowValue(dtExcel, dr, currentRow, currentRow.LastCellNum);
                }
                return dtExcel;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 初始化DataTable
        /// </summary>
        /// <returns></returns>
        private DataTable InitDataTable()
        {
            DataTable dt_excel = new DataTable();
            dt_excel.Columns.Add("A");
            dt_excel.Columns.Add("B");
       
            return dt_excel;
        }

        /// <summary>
        /// 读取到的Excel的单元格数量
        /// </summary>
        /// <param name="currentCellNum"></param>
        private void CheckExcelCellNum(int readCurrentRowCellNum)
        {
            Assert.IsTrue(readCurrentRowCellNum > DEFAULT_CHECK_CELL_NUM, "Excel单元格列数超过：" + DEFAULT_CHECK_CELL_NUM + "列");
        }
        /// <summary>
        /// 给DataTable动态赋值
        /// </summary>
        /// <param name="dr">DataTable当前行</param>
        /// <param name="currentRow">Excel当前行数据</param>
        /// <param name="currentCellNum">Excel的列数</param>
        private void SetCurrentRowValue(DataTable dtExcel, DataRow dr, IRow currentRow, int currentCellNum)
        {
            dr.BeginEdit();
            for (int j = 0; j < currentCellNum; j++)
            {
                if (j >= DEFAULT_CHECK_CELL_NUM) break;
                dr[j] = currentRow.GetCell(j).ToString().Trim();
            }
            dr.EndEdit();
            dtExcel.Rows.Add(dr);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
